﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class Straight
{
	public GameObject point_1;
	public GameObject point_2;
}

public class GameManager : MonoBehaviour {

	private GameObject origin;
	private GameObject destination;
	private bool checkDraw = false;
	private int countPoint = 0;
	public List<Straight> listStraight;
	public List<GameObject> listObjDrawLine;
	public List<GameObject> listObjPoint;
	public List<GameObject> listObjAutoPoint;
	public List<Straight> listSAutotraight;
	private ConfigLevelItem configLevelItem;
	private int level = 1;
	// Use this for initialization
	void Start () {
		AvConfigManager.Instance.LoadAll (); 
		listObjDrawLine = new List<GameObject> ();
		listStraight = new List<Straight> ();
		listSAutotraight = new List<Straight> ();
		AutoDrawLine ();
	}
	
	// Update is called once per frame
	void Update () {

	}

	//Click event point
	public void SetPoint(GameObject obj)
	{
		if(countPoint <= 2)
			countPoint++;

		switch (countPoint) {
		case 1:
			origin = obj;
			break;
		case 2:
			destination = obj;
			checkDraw = false;
			//check double click
			if (origin.name == destination.name) {
				countPoint--;
				return;
			}

			//Check Duplicate
			foreach (Straight item in listStraight) {
				if (item.point_1.name == origin.name && item.point_2.name == destination.name) {
					checkDraw = true;
				}

				if (item.point_1.name == destination.name && item.point_2.name == origin.name) {
					checkDraw = true;
				}
			}

			if (checkDraw == false) {
				GameObject insObj = Instantiate (Resources.Load ("Prefab/DrawLine")) as GameObject;
				insObj.tag = "straight";
				insObj.GetComponent<DrawLine> ().InitLineRender (origin.transform, destination.transform);
				insObj.name = origin.name + "-" + destination.name;
				listObjDrawLine.Add (insObj);

				Straight straight = new Straight ();
				straight.point_1 = origin;
				straight.point_2 = destination;
				listStraight.Add (straight);
			} 
			countPoint = 0;
			break;
		}
	}

	//Auto draw linr square left
	void AutoDrawLine()
	{
		configLevelItem = AvConfigManager.configLevel.GetConfigLevelItem (level);
		string[] listOrigin = configLevelItem.origin.Split ('+');
		string[] listDestination = configLevelItem.destination.Split ('+');

		for (int i = 0; i < listOrigin.Length; i++) {
			Straight straight = new Straight ();
			straight.point_1 = listObjAutoPoint [int.Parse(listOrigin [i].Remove (0, 5))-1];
			straight.point_2 = listObjAutoPoint [int.Parse(listDestination[i].Remove(0,5)) - 1];
			listSAutotraight.Add (straight);
		}
			
		foreach (Straight item in listSAutotraight) {
			GameObject insObj = Instantiate (Resources.Load ("Prefab/DrawLine")) as GameObject;
			insObj.GetComponent<DrawLine> ().InitLineRender (item.point_1.transform, item.point_2.transform, false);
		}
	}

	//Check collider point red
	public void EnableDisableListPoint(bool check)
	{
		
		foreach (GameObject item in listObjPoint) {
			item.GetComponent<SphereCollider> ().enabled = check;
		}
	}

	//Check collider straight
	public void EnableDisableListDrawLine(bool check)
	{
		foreach (GameObject item in listObjDrawLine) {
			item.transform.FindChild ("Collider").GetComponent<BoxCollider> ().enabled = check;
		}
	}

	//Check True False after user draw imge
	public void CheckTrueFasle(Text result)
	{
		int trueStraight = 0;
		configLevelItem = AvConfigManager.configLevel.GetConfigLevelItem (level);
		string[] listOrigin = configLevelItem.origin.Split ('+');
		string[] listDestination = configLevelItem.destination.Split ('+');
		Debug.Log ("CheckTrueFasle");

		for (int x = 0; x < listStraight.Count; x++) 
		{
			for (int i = 0; i < listOrigin.Length; i++) 
			{
				string point_1 = listStraight [x].point_1.name.Remove (0, 6);
				string point_2 = listStraight [x].point_2.name.Remove (0, 6);
				string origin = listOrigin [i].Remove (0, 5);
				string destination = listDestination [i].Remove (0, 5);

				if (point_1 == origin && point_2 == destination)
				{
					trueStraight++;
				}
					
				if (point_1 == destination && point_2 == origin) 
				{
					trueStraight++;
				}
			}
		}
			
		if (trueStraight == listOrigin.Length && trueStraight == listStraight.Count) {
			Debug.Log ("Result: True");
			result.text = "RESULT: TRUE - LINE TRUE: " + trueStraight + " - LINE FALSE: " + (listStraight.Count - trueStraight);
		} else {
			Debug.Log ("Result: False");
			result.text = "RESULT: FALSE - LINE TRUE: " + trueStraight + " - LINE FALSE: " + (listStraight.Count - trueStraight);
		}
	}

	public void ResetWhenChangeEarser()
	{
		countPoint = 0;
	}
}
